{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import sklearn.model_selection\n",
    "import sklearn.model_selection\n",
    "from load_data import *\n",
    "from keras.models import Model\n",
    "from keras.layers import Input, Dense\n",
    "from sklearn.preprocessing import normalize\n",
    "from load_data import load_all_feature_vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done with  1\n",
      "Done with  2\n",
      "Done with  3\n",
      "Done with  4\n",
      "Done with  5\n",
      "Done with  7\n",
      "Done with  9\n",
      "Done with  11\n",
      "Done with  14\n",
      "Done with  16\n",
      "Mistake Theta\n",
      "Mistake Alpha\n",
      "Mistake Theta\n",
      "Mistake Alpha\n",
      "Mistake Theta\n",
      "Mistake Alpha\n",
      "Done with  17\n",
      "Done with  18\n",
      "Done with  19\n"
     ]
    }
   ],
   "source": [
    "seiz_ad, non_seiz_ad = load_all_feature_vectors(all_bands=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "from openpyxl import load_workbook\n",
    "\n",
    "def save_ann_data_to(data, path):\n",
    "    if os.path.isfile(path):\n",
    "        book = load_workbook(path)\n",
    "        writer = pd.ExcelWriter(path, engine='openpyxl')\n",
    "        writer.book = book\n",
    "\n",
    "        df = pd.DataFrame(data, columns=['Loss', 'binary_accuracy', 'precision', 'specificity'])\n",
    "        df.to_excel(writer)\n",
    "\n",
    "        writer.save()\n",
    "        writer.close()\n",
    "\n",
    "    else:\n",
    "        df = pd.DataFrame(data,\n",
    "                          columns=['Loss', 'binary_accuracy', 'precision', 'specificity'])\n",
    "        df.to_excel(path)\n",
    "\n",
    "\n",
    "def save_svm_data_to(data, path):\n",
    "    if os.path.isfile(path):\n",
    "        book = load_workbook(path)\n",
    "        writer = pd.ExcelWriter(path, engine='openpyxl')\n",
    "        writer.book = book\n",
    "\n",
    "        df = pd.DataFrame(data, columns=['Mean Accuracy', 'Mean precision', 'Sensitivity', 'Specificity'])\n",
    "        df.to_excel(writer)\n",
    "\n",
    "        writer.save()\n",
    "        writer.close()\n",
    "\n",
    "    else:\n",
    "        df = pd.DataFrame(data,\n",
    "                          columns=['Mean Accuracy', 'Mean precision', 'Sensitivity', 'Specificity'])\n",
    "        df.to_excel(path)\n",
    "\n",
    "def save_rdf_data_to(data, path):\n",
    "    if os.path.isfile(path):\n",
    "        book = load_workbook(path)\n",
    "        writer = pd.ExcelWriter(path, engine='openpyxl')\n",
    "        writer.book = book\n",
    "\n",
    "        df = pd.DataFrame(data, columns=['Mean Accuracy', 'Precision', 'Sensitivity', 'Specificity'])\n",
    "        df.to_excel(writer)\n",
    "\n",
    "        writer.save()\n",
    "        writer.close()\n",
    "\n",
    "    else:\n",
    "        df = pd.DataFrame(data,\n",
    "                          columns=['Mean Accuracy', 'Precision', 'Sensitivity', 'Specificity'])\n",
    "        df.to_excel(path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras.backend as K\n",
    "\n",
    "def precision(y_true, y_pred):\n",
    "    \"\"\"Precision metric.\n",
    "    Only computes a batch-wise average of precision.\n",
    "    Computes the precision, a metric for multi-label classification of\n",
    "    how many selected items are relevant.\n",
    "    \"\"\"\n",
    "    neg_y_true = 1 - y_true\n",
    "    neg_y_pred = 1 - y_pred\n",
    "    fp = K.sum(neg_y_true * y_pred)\n",
    "    tn = K.sum(neg_y_true * neg_y_pred)\n",
    "    tp = K.sum(y_true * y_pred)\n",
    "    fn = K.sum(neg_y_pred * y_pred)\n",
    "    precision = tp / (tp + fp + K.epsilon())\n",
    "    return precision\n",
    "\n",
    "def specificity(y_true, y_pred):\n",
    "    \"\"\"\n",
    "    param:\n",
    "    y_pred - Predicted labels\n",
    "    y_true - True labels \n",
    "    Returns:\n",
    "    Specificity score\n",
    "    \"\"\"\n",
    "    neg_y_true = 1 - y_true\n",
    "    neg_y_pred = 1 - y_pred\n",
    "    fp = K.sum(neg_y_true * y_pred)\n",
    "    tn = K.sum(neg_y_true * neg_y_pred)\n",
    "    specificity = tn / (tn + fp + K.epsilon())\n",
    "    return specificity\n",
    "\n",
    "def sensitivity(y_true, y_pred):\n",
    "    neg_y_true = 1 - y_true\n",
    "    neg_y_pred = 1 - y_pred\n",
    "    fp = K.sum(neg_y_true * y_pred)\n",
    "    tn = K.sum(neg_y_true * neg_y_pred)\n",
    "    tp = K.sum(y_true * y_pred)\n",
    "    fn = K.sum(neg_y_pred * y_pred)\n",
    "    sensitivity = tp / (tp + fn + K.epsilon())\n",
    "    return sensitivity\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cutting(seiz, non_seiz, factor):\n",
    "    index = np.random.randint(factor * len(seiz), len(non_seiz) - factor * len(seiz) - 1)\n",
    "    stop = index + int(factor * len(seiz))\n",
    "    non_seiz_vector = non_seiz[index:stop][:]\n",
    "\n",
    "    #concattenate the newly created vecotrs for seiz and non_seiz\n",
    "    seizure = []\n",
    "    for i in range(len(seiz)):\n",
    "        if len(seiz[i]) > 0:\n",
    "            seizure.append(seiz[i])\n",
    "    data = np.concatenate((np.asarray(seizure), np.asarray(non_seiz_vector)), axis=0)\n",
    "\n",
    "    seiz_labels = np.ones(len(seizure))\n",
    "    non_seiz_labels = np.zeros(len(non_seiz_vector))\n",
    "    labels = np.concatenate((seiz_labels, non_seiz_labels))\n",
    "\n",
    "    return normalize(data, 'l1'), labels\n",
    "\n",
    "def norm(seiz, non_seiz):\n",
    "    #create labels\n",
    "    seizure = []\n",
    "    for i in range(len(seiz)):\n",
    "        if len(seiz[i]) > 0:\n",
    "            seizure.append(seiz[i])\n",
    "\n",
    "    seiz_labels = np.ones(len(seizure))\n",
    "    non_seiz_labels = np.zeros(len(non_seiz))\n",
    "    labels = np.concatenate((seiz_labels, non_seiz_labels))\n",
    "\n",
    "    #concattenate the newly created vecotrs for seiz and non_seiz\n",
    "    data = np.concatenate((np.asarray(seizure), np.asarray(non_seiz)), axis=0)\n",
    "\n",
    "    return normalize(data, 'l1'), labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "metrics = ['binary_accuracy',precision, sensitivity, specificity]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_all_data_alpha_delta = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = norm(seiz_ad.copy(), non_seiz_ad.copy())\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "\n",
    "    inp = Input(shape=(6,))\n",
    "    hidden = Dense(6, activation='sigmoid')(inp)\n",
    "    outp = Dense(1)(hidden)\n",
    "    model = Model(inputs=inp, outputs=outp)\n",
    "\n",
    "    model.compile(optimizer='SGD',\n",
    "                  loss='mean_squared_error',\n",
    "                  metrics=metrics)  #Using MSE as lossfunction\n",
    "\n",
    "    model.fit(x=ann_x_train, y=ann_y_train,\n",
    "              batch_size=3,\n",
    "              epochs=1000,\n",
    "              shuffle=True,\n",
    "              verbose=False)\n",
    "    score = model.evaluate(x=ann_x_test, y=ann_y_test, verbose=False)\n",
    "    ann_all_data_alpha_delta.append(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_ann_data_to(ann_all_data_alpha_delta, 'ANN_all_data_alpha_delta.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_all_data_alpha_delta_3t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), factor=3)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "\n",
    "    inp = Input(shape=(6,))\n",
    "    hidden = Dense(6, activation='sigmoid')(inp)\n",
    "    outp = Dense(1)(hidden)\n",
    "    model = Model(inputs=inp, outputs=outp)\n",
    "\n",
    "    model.compile(optimizer='SGD',\n",
    "                  loss='mean_squared_error',\n",
    "                  metrics=metrics)  #Using MSE as lossfunction\n",
    "\n",
    "    model.fit(x=ann_x_train, y=ann_y_train,\n",
    "              batch_size=3,\n",
    "              epochs=1000,\n",
    "              shuffle=True,\n",
    "              verbose=False)\n",
    "    score = model.evaluate(x=ann_x_test, y=ann_y_test, verbose=False)\n",
    "    ann_all_data_alpha_delta_3t1.append(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_ann_data_to(ann_all_data_alpha_delta_3t1, 'ANN_3t1_alpha_delta.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_all_data_alpha_delta_25t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), factor=2.5)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "\n",
    "    inp = Input(shape=(6,))\n",
    "    hidden = Dense(6, activation='sigmoid')(inp)\n",
    "    outp = Dense(1)(hidden)\n",
    "    model = Model(inputs=inp, outputs=outp)\n",
    "\n",
    "    model.compile(optimizer='SGD',\n",
    "                  loss='mean_squared_error',\n",
    "                  metrics=metrics)  #Using MSE as lossfunction\n",
    "\n",
    "    model.fit(x=ann_x_train, y=ann_y_train,\n",
    "              batch_size=3,\n",
    "              epochs=1000,\n",
    "              shuffle=True,\n",
    "              verbose=False)\n",
    "    score = model.evaluate(x=ann_x_test, y=ann_y_test, verbose=False)\n",
    "    ann_all_data_alpha_delta_25t1.append(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_ann_data_to(ann_all_data_alpha_delta_25t1, 'ANN_25t1_alpha_delta.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_all_data_alpha_delta_2t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), factor=2)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "\n",
    "    inp = Input(shape=(6,))\n",
    "    hidden = Dense(6, activation='sigmoid')(inp)\n",
    "    outp = Dense(1)(hidden)\n",
    "    model = Model(inputs=inp, outputs=outp)\n",
    "\n",
    "    model.compile(optimizer='SGD',\n",
    "                  loss='mean_squared_error',\n",
    "                  metrics=metrics)  #Using MSE as lossfunction\n",
    "\n",
    "    model.fit(x=ann_x_train, y=ann_y_train,\n",
    "              batch_size=3,\n",
    "              epochs=1000,\n",
    "              shuffle=True,\n",
    "              verbose=False)\n",
    "    score = model.evaluate(x=ann_x_test, y=ann_y_test, verbose=False)\n",
    "    ann_all_data_alpha_delta_2t1.append(score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_ann_data_to(ann_all_data_alpha_delta_2t1, 'ANN_2t1_alpha_delta.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_rdf_scores = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = norm(seiz_ad.copy(), non_seiz_ad.copy())\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    rdf = sklearn.ensemble.RandomForestClassifier()\n",
    "    rdf.fit(ann_x_train, ann_y_train)\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "    print(y_pred)\n",
    "    rdf_cm = confusion_matrix(ann_y_test, y_pred)\n",
    "\n",
    "    tn, fp, fn, tp = rdf_cm.ravel()\n",
    "\n",
    "    prec = sklearn.metrics.precision_score(ann_y_test, y_pred)\n",
    "    sensitivity = tp / (tp + fn)\n",
    "    specificity = tn / (tn + fp)\n",
    "\n",
    "    all_rdf_scores.append([rdf.score(ann_x_test, ann_y_test), prec, sensitivity, specificity])\n",
    "\n",
    "save_rdf_data_to(all_rdf_scores,'RDF_alpha_delta_all.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_rdf_scores_3t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(),3)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    rdf = sklearn.ensemble.RandomForestClassifier()\n",
    "    rdf.fit(ann_x_train, ann_y_train)\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "    print(y_pred)\n",
    "    rdf_cm = confusion_matrix(ann_y_test, y_pred)\n",
    "\n",
    "    tn, fp, fn, tp = rdf_cm.ravel()\n",
    "\n",
    "    prec = sklearn.metrics.precision_score(ann_y_test, y_pred)\n",
    "    sensitivity = tp / (tp + fn)\n",
    "    specificity = tn / (tn + fp)\n",
    "\n",
    "    all_rdf_scores_3t1.append([rdf.score(ann_x_test, ann_y_test), prec, sensitivity, specificity])\n",
    "\n",
    "save_rdf_data_to(all_rdf_scores_3t1,'RDF_alpha_delta_3t1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_rdf_scores_25t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), 2.5)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    rdf = sklearn.ensemble.RandomForestClassifier()\n",
    "    rdf.fit(ann_x_train, ann_y_train)\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "    print(y_pred)\n",
    "    rdf_cm = confusion_matrix(ann_y_test, y_pred)\n",
    "\n",
    "    tn, fp, fn, tp = rdf_cm.ravel()\n",
    "\n",
    "    prec = sklearn.metrics.precision_score(ann_y_test, y_pred)\n",
    "    sensitivity = tp / (tp + fn)\n",
    "    specificity = tn / (tn + fp)\n",
    "\n",
    "    all_rdf_scores_25t1.append([rdf.score(ann_x_test, ann_y_test), prec, sensitivity, specificity])\n",
    "\n",
    "save_rdf_data_to(all_rdf_scores_25t1,'RDF_alpha_delta_25t1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_rdf_scores_2t1 = []\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), 2)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    rdf = sklearn.ensemble.RandomForestClassifier()\n",
    "    rdf.fit(ann_x_train, ann_y_train)\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "\n",
    "    y_pred = rdf.predict(ann_x_test)\n",
    "    print(y_pred)\n",
    "    rdf_cm = confusion_matrix(ann_y_test, y_pred)\n",
    "\n",
    "    tn, fp, fn, tp = rdf_cm.ravel()\n",
    "\n",
    "    prec = sklearn.metrics.precision_score(ann_y_test, y_pred)\n",
    "    sensitivity = tp / (tp + fn)\n",
    "    specificity = tn / (tn + fp)\n",
    "\n",
    "    all_rdf_scores_2t1.append([rdf.score(ann_x_test, ann_y_test), prec, sensitivity, specificity])\n",
    "\n",
    "save_rdf_data_to(all_rdf_scores_2t1,'RDF_alpha_delta_2t1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_svm_scores = []\n",
    "\n",
    "for i in range(100):\n",
    "    ann_data, labels = norm(seiz_ad.copy(), non_seiz_ad.copy())\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    \n",
    "    svm = sklearn.svm.SVC(kernel='rbf')\n",
    "    svm.fit(ann_x_train,ann_y_train)\n",
    "    \n",
    "    y_pred = svm.predict(ann_x_test)\n",
    "    cm = confusion_matrix(ann_y_test, y_pred)\n",
    "    tn, fp, fn, tp = cm.ravel()\n",
    "\n",
    "    sensitivity = tp/(tp+fn)\n",
    "    specificity = tn/(tn+fp)\n",
    "    \n",
    "    svm_y_precision = svm.decision_function(ann_x_test)\n",
    "    average_svm_precision = sklearn.metrics.average_precision_score(ann_y_test, svm_y_precision)    \n",
    "    all_svm_scores.append([svm.score(ann_x_test,ann_y_test), average_svm_precision, sensitivity, specificity])\n",
    "    \n",
    "save_svm_data_to(all_svm_scores,'SVM_alpha_delta_all.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_svm_scores_3t1 = []\n",
    "\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), 3)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    \n",
    "    svm = sklearn.svm.SVC(kernel='rbf')\n",
    "    svm.fit(ann_x_train,ann_y_train)\n",
    "    \n",
    "    y_pred = svm.predict(ann_x_test)\n",
    "    cm = confusion_matrix(ann_y_test, y_pred)\n",
    "    tn, fp, fn, tp = cm.ravel()\n",
    "\n",
    "    sensitivity = tp/(tp+fn)\n",
    "    specificity = tn/(tn+fp)\n",
    "    \n",
    "    svm_y_precision = svm.decision_function(ann_x_test)\n",
    "    average_svm_precision = sklearn.metrics.average_precision_score(ann_y_test, svm_y_precision)    \n",
    "    all_svm_scores_3t1.append([svm.score(ann_x_test,ann_y_test), average_svm_precision, sensitivity, specificity])\n",
    "    \n",
    "save_svm_data_to(all_svm_scores_3t1,'SVM_alpha_delta_3t1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_svm_scores_25t1 = []\n",
    "\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), 2.5)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    \n",
    "    svm = sklearn.svm.SVC(kernel='rbf')\n",
    "    svm.fit(ann_x_train,ann_y_train)\n",
    "    \n",
    "    y_pred = svm.predict(ann_x_test)\n",
    "    cm = confusion_matrix(ann_y_test, y_pred)\n",
    "    tn, fp, fn, tp = cm.ravel()\n",
    "\n",
    "    sensitivity = tp/(tp+fn)\n",
    "    specificity = tn/(tn+fp)\n",
    "    \n",
    "    svm_y_precision = svm.decision_function(ann_x_test)\n",
    "    average_svm_precision = sklearn.metrics.average_precision_score(ann_y_test, svm_y_precision)    \n",
    "    all_svm_scores_25t1.append([svm.score(ann_x_test,ann_y_test), average_svm_precision, sensitivity, specificity])\n",
    "    \n",
    "save_svm_data_to(all_svm_scores_25t1,'SVM_alpha_delta_25t1.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_svm_scores_2t1 = []\n",
    "\n",
    "for i in range(100):\n",
    "    ann_data, labels = cutting(seiz_ad.copy(), non_seiz_ad.copy(), 2)\n",
    "    ann_x_train, ann_x_test, ann_y_train, ann_y_test = sklearn.model_selection.train_test_split(ann_data, labels)\n",
    "    \n",
    "    svm = sklearn.svm.SVC(kernel='rbf')\n",
    "    svm.fit(ann_x_train,ann_y_train)\n",
    "    \n",
    "    y_pred = svm.predict(ann_x_test)\n",
    "    cm = confusion_matrix(ann_y_test, y_pred)\n",
    "    tn, fp, fn, tp = cm.ravel()\n",
    "\n",
    "    sensitivity = tp/(tp+fn)\n",
    "    specificity = tn/(tn+fp)\n",
    "    \n",
    "    svm_y_precision = svm.decision_function(ann_x_test)\n",
    "    average_svm_precision = sklearn.metrics.average_precision_score(ann_y_test, svm_y_precision)    \n",
    "    all_svm_scores_2t1.append([svm.score(ann_x_test,ann_y_test), average_svm_precision, sensitivity, specificity])\n",
    "    \n",
    "save_svm_data_to(all_svm_scores_2t1,'SVM_alpha_delta_2t1.xlsx')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
